global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A20b_classification_autostats_b.log, replace name(tabl)

capture noi { 
    
    * which class of codes (ipc4 vs ipc6XX vs ipc4_pairs) are important for the classification (panel a)

    texdoc init ${tab_dir}/Appendix/Table_A20b_classification_autostats_b.tex, replace
    texdoc stlog, nolog

    use ${final_dir}/patlevel_classification_stats.dta, clear

    foreach q in 80 90 95 {
        foreach comp in auto autm CNC robo {
            foreach z in 80 90 95 { 
                quietly: estpost tabstat is`comp'`q' if  isauto`z' == 1
                quietly: matrix list e(mean)
                sleep 700
                texdoc local is`comp'`q'_ifa`z' : display %10.1f (e(mean)[1,1] * 100)
            }
        }
    }

    * output table (via texdoc)
    texdoc local notes "This table shows the share of innovations classified as automation innovation through 6-digit C/IPC codes, 4-digit C/IPC pairs or 4-digit C/IPC - G05/G06 pairs. The statistics are computed on biadic patents from 1997-2011."
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{}l*{4}{r}@{}}
    \toprule
    Sources / Patents  & Auto80 & Auto90 & Auto95 \\
    \midrule
    Auto80      &   `isauto80_ifa80'\%      & `isauto80_ifa90'\%    &   `isauto80_ifa95'\%    \\
    Automat*80  &   `isautm80_ifa80'\%      & `isautm80_ifa90'\%    &   `isautm80_ifa95'\%    \\
    CNC80       &   `isCNC80_ifa80'\%       & `isCNC80_ifa90'\%     &   `isCNC80_ifa95'\%     \\
    Robot80     &   `isrobo80_ifa80'\%      & `isrobo80_ifa90'\%    &   `isrobo80_ifa95'\%    \\
    \noalign{\smallskip}
    Auto90      &   `isauto90_ifa80'\%      & `isauto90_ifa90'\%    &   `isauto90_ifa95'\%    \\
    Automat*90  &   `isautm90_ifa80'\%      & `isautm90_ifa90'\%    &   `isautm90_ifa95'\%    \\
    CNC90       &   `isCNC90_ifa80'\%       & `isCNC90_ifa90'\%     &   `isCNC90_ifa95'\%     \\
    Robot90     &   `isrobo90_ifa80'\%      & `isrobo90_ifa90'\%    &   `isrobo90_ifa95'\%    \\
    \noalign{\smallskip}
    Auto95      &   `isauto95_ifa80'\%      & `isauto95_ifa90'\%    &   `isauto95_ifa95'\%    \\
    Automat*95  &   `isautm95_ifa80'\%      & `isautm95_ifa90'\%    &   `isautm95_ifa95'\%    \\
    CNC95       &   `isCNC95_ifa80'\%       & `isCNC95_ifa90'\%     &   `isCNC95_ifa95'\%     \\
    Robot95     &   `isrobo95_ifa80'\%      & `isrobo95_ifa90'\%    &   `isrobo95_ifa95'\%    \\

    \bottomrule
    \end{tabular}
    tex*/
    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}